//yarn add uuid@8.3.2
//yarn add moment@2.29.1
const { v4: uuidv4 } = require("uuid");
// const moment = require("moment");

/**
 * 表单模型
 */
module.exports = (app) => {
  const { STRING, INTEGER, UUID, TEXT, DATE } = app.Sequelize;
  const Note = app.model.define(
    "note",
    {
      id: {
        type: UUID,
        unique: true,
        primaryKey: true,
        allowNull: false,
        defaultValue: () => {
          return uuidv4().replace(/-/g, "");
        },
      },
      title: {
        type: STRING,
        allowNull: false,
      },
      content: {
        type: TEXT,
        allowNull: false,
      },
      created_time: {
        type:DATE,
        allowNull: false,
      },
      updated_time: {
        type: DATE,
        allowNull: false,
      },
      public: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
      delFlag: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
    },
    {
      timestamps: false,
      freezeTableName: true,
    }
  );

  // 表关联的字段
  Note.associate = function () {
    app.model.Note.belongsTo(app.model.User, {
      foreignKey: "user_id",
      targetKey: "id",
    });
  };

  return Note;
};
